package com.zhang.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.IService;
import com.zhang.domain.ArticleComment;
import com.zhang.domain.SubjectArticle;
import com.zhang.entity.vo.SubjectArticleVo;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import xin.altitude.cms.common.util.BeanCopyUtils;
import xin.altitude.cms.common.util.EntityUtils;
import xin.altitude.cms.common.util.SpringUtils;

public interface ISubjectArticleService extends IService<SubjectArticle>{
    /** 通过方法调用来解决循环依赖问题 */
    default IArticleCommentService getArticleCommentService() {
        return SpringUtils.getBean(IArticleCommentService.class);
    }
    /**查询用户实体类Vo*/    default SubjectArticleVo getOneVo(Long id) {
        SubjectArticleVo subjectArticleVo = EntityUtils.toObj(getById(id), SubjectArticleVo::new);
        List<ArticleComment> articleCommentList = getArticleCommentService().list(Wrappers.lambdaQuery(ArticleComment.class).eq(ArticleComment::getArticleId, subjectArticleVo.getId()));
        subjectArticleVo.setArticleCommentList(articleCommentList);
        return subjectArticleVo;
    }
    /**查询实体类Vo列表*/    default List<SubjectArticleVo> listVo(SubjectArticle subjectArticle) {
        List<SubjectArticleVo> subjectArticleVoList = EntityUtils.toList(list(Wrappers.lambdaQuery(subjectArticle)), SubjectArticleVo::new);
        Set<Long> subjectArticleIds = EntityUtils.toSet(subjectArticleVoList, SubjectArticleVo::getId);
        List<ArticleComment> articleCommentList = getArticleCommentService().list(Wrappers.lambdaQuery(ArticleComment.class).in(ArticleComment::getArticleId, subjectArticleIds));
        Map<Long, List<ArticleComment>> map = articleCommentList.stream().collect(Collectors.groupingBy(ArticleComment::getArticleId));
        subjectArticleVoList.forEach(e -> e.setArticleCommentList(map.get(e.getId())));
        return subjectArticleVoList;
    }
    /**分页查询实体类Vo*/    default IPage<SubjectArticleVo> pageVo(IPage<SubjectArticle> page, SubjectArticle subjectArticle) {
        IPage<SubjectArticleVo> subjectArticleVoPage = EntityUtils.toPage(page(page, Wrappers.lambdaQuery(subjectArticle)), SubjectArticleVo::new);
        Set<Long> subjectArticleIds = EntityUtils.toSet(subjectArticleVoPage.getRecords(), SubjectArticleVo::getId);
        List<ArticleComment> articleCommentList = getArticleCommentService().list(Wrappers.lambdaQuery(ArticleComment.class).in(ArticleComment::getArticleId, subjectArticleIds));
        Map<Long, List<ArticleComment>> map = articleCommentList.stream().collect(Collectors.groupingBy(ArticleComment::getArticleId));
        subjectArticleVoPage.getRecords().forEach(e -> e.setArticleCommentList(map.get(e.getId())));
        return subjectArticleVoPage;
    }
}
